var animation; // 动画实体
var ani_index = 0; // 动画执行次数index（当前执行了多少次）
var aniIntervalId = -1; // 动画定时任务id，通过setInterval来达到无限缩放，记录id，用于结束定时任务
Page({
  data: {
    swiper: {
      imgUrls: [
        {
          images: 'https://fcdn.qiushilaile.com/adx/app-70951611748104550.png',
        },
        {
          images: 'https://fcdn.qiushilaile.com/adx/app-72961611749644592.png',
          value: 2,
        },
        // {
        //   images: 'https://fcdn.qiushilaile.com/adx/app-8721611749667624.png',
        //   value: 3,
        // },
        {
          images: 'https://fcdn.qiushilaile.com/game/default-47351611749850972.png',
          value: 4,
        },
        // {
        //   images: 'https://fcdn.qiushilaile.com/game/default-21181611749955989.png',
        //   value: 5,
        // },
        {
          images: 'https://fcdn.qiushilaile.com/game/default-45091611750768174.png',
          value: 6,
        },
        {
          images: 'https://fcdn.qiushilaile.com/game/default-17351611751767943.png',
          // url: '/pages/dengmi/denglong/denglong',
          value: 7,
        },
        // {
        //   images: 'https://fcdn.qiushilaile.com/game/default-47351611749850972.png',
        //   // url: '/pages/dengmi/denglong/denglong',
        //   value: 8,
        // },
      ],
    },
    widthProgress: 0,
    testLoadingLayer: true,
    currentIndex: 0,
    close: 0,
    lead: false,
    on: true   // 控制音乐的状态，以及图标是否旋转
  },
  onLoad(query) {
    let that = this
    setTimeout(() => {
      this.setData({
        lead: false
      })
    }, 7000)
    let openId = tt.getStorageSync('openId')
    let flag = tt.getStorageSync('flag')
    if (flag) {
      console.log('flag存在', flag)
      tt.reLaunch({
        url: '../../pages/index/index'
      });
    } else {
      console.log('flag不存在')
      tt.setStorageSync('flag', openId);
    }
    tt.getSystemInfo({
      success: (res) => {
        that.setData({
          windowHeight: res.windowHeight
        })
      }
    });
  },
  onReady() {
    ani_index = 0;
    aniIntervalId = -1;
    this.data.animation = '';
    animation = tt.createAnimation({
      duration: 800,
      timingFunction: 'linear', // "linear","ease","ease-in","ease-in-out","ease-out","step-start","step-end"
      delay: 0, // 0延迟
      transformOrigin: '50% 50% 0'
    })
    this.startAnimationInterval()
  },
  stop() {
    this.back.pause(); // 点击音乐图标后出发的操作
    this.setData({
      on: !this.data.on
    })
    if (this.data.on) {
      this.back.src = "https://fcdn.qiushilaile.com/adx/app-9521611320228939.mp3"
      this.back.play()
      this.back.onPlay((res) => {
        console.log(res)
      })
    } else {
      this.back.pause()
    }
  },
  aniScale: function (n) { // 缩放动画
    if (n % 2 == 0) {
      animation.scale(1.15).step();
    } else {
      animation.scale(1.0).step();
    }
    this.setData({
      animation: animation.export()
    });
  },
  startAnimationInterval: function () { // 开始缩放
    var that = this;
    that.aniScale(++ani_index); // 进行一次缩放
    aniIntervalId = setInterval(function () {
      that.aniScale(++ani_index);
    }, 800); // 每间隔0.8s进行一次缩放
  },
  toIndex() {
    this.back.pause();
    tt.reLaunch({
      url: '../../pages/index/index'
    });
  },
  handleChange: function (e) {
    // console.log(e.detail)
    this.setData({
      currentIndex: e.detail.current
    })
  },
  onClose() {
    let that = this
    this.setData({
      close: 1
    })
    that.back = tt.getBackgroundAudioManager() //创建背景音乐
    that.back.src = "https://fcdn.qiushilaile.com/adx/app-9521611320228939.mp3"
    that.back.title = '小试牛刀'   // 标题为必选项
    that.back.onEnded((res) => {
      that.setData({
        on: !that.data.on
      })
      this.back.play()
    })
  },
})
